<script setup lang="ts">
// components
import comDetailItem from '@/components/common/com-detailItem/com-detailItem.vue';
import comIcon from '@/components/common/com-icon/com-icon.vue';
import comNavigation from '@/components/common/com-navigationBar/com-navigation.vue';
// vue
import { useRoute, useRouter } from 'vue-router';
// api
import { onMounted, ref } from 'vue';
import type { DocDetail } from '@/types/healthEncyclopedia';
import { getDocDetailBydocIdAPI, getPatientKnowledgePageAPI, postLikeAPI } from '@/api/doctorDetail';
import type { rowss } from '@/types/types';
import { showToast } from 'vant';
const route = useRoute()
const router = useRouter()
// 文章
const currentPage = ref<number>(1)
const pageSize = ref<number>(10)
const id = route.params.keyword as string
// 医生数据
const nomore = ref(false)
const docDetailData = ref<DocDetail>()
const getDocDetailBydocIdData = async () => {
  let res = await getDocDetailBydocIdAPI(id)
  if (res.data.code === 10000) {
    docDetailData.value = res.data.data
    localStorage.setItem('docDetailData', JSON.stringify(docDetailData.value))
  }
}
// 医生文章
const docArticleDetailList = ref<rowss[]>()
const getknowledgePageData = async () => {
  let res = await getPatientKnowledgePageAPI(currentPage.value, pageSize.value, id)
  if (res.data.code === 10000) {
    docArticleDetailList.value = res.data.data.rows
  }
  if (res.data.data.rows.length < pageSize.value) nomore.value = !nomore.value
}
onMounted(() => {
  getDocDetailBydocIdData()
  getknowledgePageData()
})
// 去文章详情页
const goTopicDetail = (val: string) => {
  router.push(`/articleDetail/${val}`)
}
// 关注
const onFollow = async () => {
  let res = await postLikeAPI(id, 'doc')
  console.log(res);
  if (res.data.code === 10000) {
    if (docDetailData.value!.likeFlag === 1) {
      docDetailData.value!.likeFlag = 0
      showToast({
        message: "取消关注成功"
      })
    } else {
      docDetailData.value!.likeFlag = 1
      showToast({
        message: "关注成功"
      })
    }
  }

}
const goToDescribeIllness = () => {
  router.push('/describeIllness')
}
</script>
<template>
  <div class="box">
    <comNavigation background="#dff4f1">
      <template v-slot:right>
        <div class="right">
          <div class="shared">
            <comIcon class="sharedIcon" name="icons-home-share"></comIcon>
          </div>
        </div>
      </template>
    </comNavigation>
    <header>
      <div class="doctorInfo">
        <p class="top">
          <img class="doctorAvatar" :src="docDetailData?.avatar" alt="">
        <div v-show="docDetailData?.likeFlag === 0" class="follow" @click="onFollow">
          <comIcon name="icons-user-add"></comIcon>关注
        </div>
        <div @click="onFollow" v-show="docDetailData?.likeFlag === 1" class="follow close">
          已关注
        </div>
        </p>
        <p class="doc-info">
          <span class="doc-name">{{ docDetailData?.name }}</span>
          <span class="doc-depName">{{ docDetailData?.depName }} | {{ docDetailData?.positionalTitles }}</span>
        </p>
        <p class="line">
        <div class="doc-grade">{{ docDetailData?.gradeName }}</div>
        <span>{{ docDetailData?.hospitalName }}</span>
        </p>
        <p class="record">
          <span class="green">{{ docDetailData?.score }}</span><span>/接诊数</span><span class="green">{{
            docDetailData?.consultationNum
            }}</span>
        </p>
      </div>

    </header>
    <div class="info-content">
      <p class="info-title">个人介绍</p>
      <p class="info-pre-title">擅长领域</p>
      <p class="info-major">{{ docDetailData?.major }}</p>
    </div>
    <div class="banner">
      <div class="bannerContent">
        <comIcon class="doctorIcon" name="icons-home-ask-doctor" />
        <div class="tuwenBox">
          <p class="tuBox">
            <span class="tu">图文问诊</span>
            <span>￥{{ docDetailData?.serviceFee }}/次</span>
          </p>
          <p class="tuwen">
            最长24小时图文多次沟通
          </p>
        </div>
        <div class="btn" @click="goToDescribeIllness">去咨询</div>
      </div>
    </div>
    <div class="evaluate">
      <div class="evaluate_header">
        <div class="left">
          患者评价<span>({{ docDetailData?.evaluateList.length }})</span>
        </div>
        <div class="right">
          评分<span>{{ docDetailData?.score }}</span>
        </div>
      </div>
      <div v-if="docDetailData?.evaluateList.length" class="evaluate" v-for="item in docDetailData?.evaluateList">
        {{ item }}
      </div>
      <div v-else class="nomore">暂无</div>
    </div>
    <div class="article-list">
      <p>他的文章</p>
      <!-- 组件 -->
      <div class="list">
        <comDetailItem @goTo="goTopicDetail(item.id)" v-for="item in docArticleDetailList" :nomore="nomore"
          :item="item">
        </comDetailItem>
        <div v-if="!nomore" class="adm-infinite-scroll">正在加载...</div>
        <div v-else class="adm-infinite-scroll">暂无更多</div>
      </div>
    </div>
    <footer>
      <button class="btn" @click="goToDescribeIllness">咨询医生</button>
    </footer>
  </div>
</template>
<style lang="scss" scoped>
.box {
  position: relative;
  padding-bottom: 65px;

  .shared {

    .sharedIcon {
      width: 25px;
    }
  }

  header {
    padding: 15px;
    background: linear-gradient(to bottom, #dff4f1, #fff);

    .doctorInfo {
      padding: 12px;
      background-color: #fff;
      border-radius: 10px;
      box-shadow: 0 0 10px 0 hsla(0, 0%, 89.8%, .5);

      .top {
        margin-bottom: 16px;
        position: relative;
        margin-top: 0;
        text-align: end;

        .doctorAvatar {
          position: absolute;
          top: -22px;
          left: 0;
          width: 55px;
          height: 55px;
          border-radius: 50%;
        }

        .follow {
          display: inline-block;
          border: 1px solid #2cb5a5;
          padding: 5px 15px;
          font-size: 11px;
          color: #2cb5a5;
          border-radius: 40px;
        }

        .close {
          background-color: #eeeeee;
          color: #c4c4c4;
          border: 1px solid transparent;
        }
      }

      .doc-info {
        height: 33px;
        margin: 10px 0 0 0;

        .doc-name {
          font-size: 21px;
          margin-right: 5px;
        }

        .doc-depName {
          font-size: 13px;
          color: #6f6f6f;

        }
      }

      .line {
        margin: 5px 0;
        display: flex;

        .doc-grade {
          margin-right: 5px;
          font-size: 10px;
          color: #fff;
          border-radius: 2px;
          padding: 1px 4px;
          background-color: #677fff;
          display: flex;
          align-items: center;
        }

        span {
          font-size: 13px;
          color: #121826;

        }
      }

      .record {
        font-size: 12px;
        color: #979797;
        margin: 0;
        display: flex;
        align-items: center;

        .green {
          color: #03bc8e;
          font-size: 16px;
          font-weight: 600;
          margin: 0 5px;
        }
      }
    }
  }

  .info-content {
    padding: 0 15px;

    .info-title {
      font-size: 16px;
      margin-bottom: 10px;
    }

    .info-pre-title {
      margin: 5px 0;
      font-size: 14px;

    }

    .info-major {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      line-height: 1.5;
      text-overflow: ellipsis;
      font-size: 12px;
      margin: 0;
    }
  }

  .banner {
    padding: 15px;

    .bannerContent {
      padding: 12px 15px;
      background: linear-gradient(281deg, #16c2a3, #1cabca);
      border-radius: 20px;
      display: flex;
      align-items: center;

      .doctorIcon {
        width: 33px;
        height: 33px;
        margin-right: 11px;
      }

      .tuwenBox {
        flex: 1;
        width: 100%;
        line-height: 1.5;

        p {
          margin: 0;
          color: #fff;
        }

        .tuwen {
          font-size: 12px;
          opacity: .7;
        }

        .tuBox {
          font-size: 12px;

          .tu {
            font-size: 16px;
            margin-right: 10px;
          }
        }
      }

      .btn {
        color: #16c2a3;
        background-color: #fff;
        border-radius: 15px;
        padding: 5px 14px;
      }
    }
  }

  .evaluate {
    padding: 0 15px;
    margin-top: 5px;

    .evaluate_header {
      display: flex;
      justify-content: space-between;
      height: 31px;

      .left {
        font-size: 16px;

        span {
          font-size: 12px;
          margin-left: 5px
        }
      }

      .right {
        font-size: 13px;

        span {
          font-size: 20px;
          color: #16c2a3;
          font-weight: 500;
          margin-left: 5px;
        }
      }
    }

    .nomore {
      margin-bottom: 11px;
    }
  }

  .article-list {
    font-size: 16px;
    padding: 0 15px;

    p {
      margin: 0 0 10px 0;
    }
  }

  footer {
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 15px;
    width: 100%;
    height: auto;
    box-sizing: border-box;
    background-color: #fff;

    .btn {
      display: block;
      width: 100%;
      height: 42px;
      border-radius: 30px;
      border: none;
      padding: 0;
      color: #fff;
      background-color: #2cb5a5;
    }
  }
}

.adm-infinite-scroll {
  color: #A2A2A2;
  background-color: #FFFFFF;
  padding: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 13px;
}
</style>
